<?php

namespace Admin\Controller;

/**
 * 后台配置控制器
 * Class ConfigController
 * @package Admin\Controller
 */
class ConfigController extends CommonController
{
    /**
     * 配置管理
     */
    public function listConfig()
    {
        $group = I('group', 0, 'intval');
        $keyword = I('keyword', '', 'trim');

        /* 查询条件初始化 */
        $where = array('state' => 0);

        if ($group != 0) {
            $where['group'] = $group;
        }

        //关键字查询
        if ($keyword != '') {
            // 解决分页跳转传过的中文参数的编码问题
            $encode = mb_detect_encoding($keyword, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
            if ($encode == 'EUC-CN') {
                $keyword = iconv('GBK', 'UTF-8', $keyword);
            }
            // 解决分页跳转传过的中文参数的编码问题 

            $where['key|name'] = array('like', '%' . $keyword . '%');
        }

        $list = $this->lists('Config', $where, 'id desc');

        $this->assign('list', $list);
        $this->assign('group', C('CONFIG_GROUP_LIST'));
        $this->assign('group_id', $group);
        $this->assign('keyword', $keyword);
        $this->display('list_config');
    }

    /**
     * 新增配置
     */
    public function addConfig()
    {
        if (IS_POST) {
            $Config = D('Config');
            $data = $Config->create();
            if ($data) {
                if ($Config->add()) {
                    S('DB_CONFIG_DATA', null);
                    $this->success('新增成功', U('listConfig'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($Config->getError());
            }
        } else {
            $this->display('add_config');
        }
    }

    /**
     * 编辑配置
     * @param integer $id
     */
    public function editConfig($id = 0)
    {
        if (IS_POST) {
            $Config = D('Config');
            $data = $Config->create();
            if ($data) {
                if ($Config->save()) {
                    S('DB_CONFIG_DATA', null);
                    $this->success('更新成功', Cookie('__forward__'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($Config->getError());
            }
        } else {
            /* 获取数据 */
            $config = M('Config')->field(true)->find($id);

            if (false === $config) {
                $this->error('获取配置信息错误');
            }
            $this->assign('config', $config);
            $this->display('edit_config');
        }
    }

    /**
     * 批量保存配置
     * @param array $config
     */
    public function save($config)
    {
        if ($config && is_array($config)) {
            $Config = M('Config');
            foreach ($config as $name => $value) {
                $map = array('key' => $name);
                $Config->where($map)->setField('value', $value);
            }
        }
        S('DB_CONFIG_DATA', null);
        $this->success('保存成功！');
    }

    /**
     * [group 获取某个标签的配置参数]
     */
    public function group()
    {
        $id = I('get.id', 1);
        $type = C('CONFIG_GROUP_LIST');
        $list = M("Config")->where(array('state' => 0, 'group' => $id))->field('id,key,name,extra,value,desc,type,sort')->order('sort')->select();
        if ($list) {
            $this->assign('list', $list);
        }
        $this->assign('id', $id);
        $this->display();
    }

    /**
     * 删除配置
     */
    public function del()
    {
        if (IS_AJAX) {
            $id = array_unique((array)I('id', 0));

            if (empty($id)) {
                $result = array('status' => 0, 'msg' => '请选择要操作的数据');
                $this->ajaxReturn($result);
            }

            $map = array('id' => array('in', $id));
            if (M('Config')->where($map)->delete()) {
                S('DB_CONFIG_DATA', null);
                $result = array('status' => 1, 'msg' => '删除成功');
            } else {
                $result = array('status' => 0, 'msg' => '删除失败');
            }

            $this->ajaxReturn($result);
        }

    }

    /**
     * [batchSort 批量排序]
     */
    public function batchSort()
    {
        if (IS_AJAX) {
            $sortData = I('sortData');

            if (empty($sortData)) {
                $result = array('status' => 0, 'msg' => '请选择要操作的数据');
                $this->ajaxReturn($result);
            }

            foreach ($sortData as $sort) {
                $sort = explode('=', $sort);
                M('Config')->where('id=' . $sort['0'])->save(array('sort' => $sort['1']));
            }

            $result = array('status' => 1, 'msg' => '排序完成');
            $this->ajaxReturn($result);
        }
    }
}